package com.reggie.console.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.reggie.acl.dataobject.Setmeal;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.model.bo.SetmealBO;
import com.reggie.service.model.request.SetmealPageRequest;
import com.reggie.service.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
@AuthRule(roles = {ReggieRole.EMPLOYEE})
public class SetmealController {

    private static final String EXPIRE_TIME = "1800";   // 30 MIN

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询
     *
     * @param page     当前页码
     * @param pageSize 每页数据条数
     * @param name     名称
     * @return 数据
     */
    @GetMapping("/page")
    public ResultDTO<IPage<SetmealBO>> page(int page, int pageSize, String name) {
        log.info("page()");
        SetmealPageRequest request = new SetmealPageRequest();
        request.setName(name);
        request.setPage(page);
        request.setPageSize(pageSize);
        IPage<SetmealBO> pageInfo = setmealService.page(request);
        return ResultBuilder.ok(pageInfo);
    }

    /**
     * 批量更新套餐态
     *
     * @param status 状态 0:停用 1:启用
     * @param ids    id集合
     * @return 消息
     */
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping("/status/{status}")
    public ResultDTO<String> status(@PathVariable int status, @RequestParam List<Long> ids) {
        log.info("status()");
        setmealService.updateStatusBatchIds(status, ids);
        return ResultBuilder.ok("套餐状态更新成功");
    }

    /**
     * 根据套餐id集合，批量删除套餐<p>
     * 只能删除停售的套餐
     *
     * @param ids 套餐id集合
     * @return 消息
     */
    @CacheEvict(value = "setmealCache", allEntries = true)
    @DeleteMapping
    public ResultDTO<String> remove(@RequestParam List<Long> ids) {
        setmealService.removeBatchIds(ids);
        return ResultBuilder.ok("套餐删除成功");
    }

    /**
     * 添加套餐
     *
     * @param setmealBO 套餐数据
     * @return 消息
     */
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping
    public ResultDTO<String> save(@RequestBody SetmealBO setmealBO) {
        log.info("remove()");
        setmealService.saveWithDishes(setmealBO);
        return ResultBuilder.ok("套餐保存成功");
    }

    /**
     * 根据套餐id，查询
     *
     * @param id 套餐id
     * @return 数据
     */
    @GetMapping("/{id}")
    public ResultDTO<SetmealBO> getById(@PathVariable long id) {
        log.info("getById()");
        SetmealBO setmealBO = setmealService.getWithDishById(id).orElse(null);
        return ResultBuilder.ok(setmealBO);
    }

    /**
     * 修改套餐数据
     *
     * @param setmealBO 套餐数据
     * @return 消息
     */
    @CacheEvict(value = "setmealCache", allEntries = true)
    @PutMapping
    public ResultDTO<String> update(@RequestBody SetmealBO setmealBO) {
        log.info("update()");
        setmealService.update(setmealBO);
        return ResultBuilder.ok("套餐修改成功");
    }

    /**
     * 根据套餐分类id、起售状态，查询套餐集合
     *
     * @param setmeal 查询条件
     * @return 数据
     */
    @Cacheable(value = "setmealCache#" + EXPIRE_TIME, sync = true,
            key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<List<SetmealBO>> list(Setmeal setmeal) {
        log.info("list()");
        List<SetmealBO> setmealBOList = setmealService.listWithDish(setmeal);
        return ResultBuilder.ok(setmealBOList);
    }

}

